import { _decorator, Component, director, EditBox, Node } from 'cc';
import { NetWorkManager } from '../Global/NetWorkManager';
import { ApiMsgEnum } from '../Common';
import DataManager from '../Global/DataManager';
import { SceneEnum } from '../Enum';
const { ccclass, property } = _decorator;

@ccclass('LoginManager')
export class LoginManager extends Component {
    input:EditBox
    

    update(deltaTime: number) {
        
    }
    onLoad() {
        this.input=this.getComponentInChildren(EditBox)
        director.preloadScene(SceneEnum.Battle)
    }
    async start() {
        //与服务器建立连接
        await NetWorkManager.Instance.connect()
    }

    async handleClick(){
        if(!NetWorkManager.Instance.isConnected){
            console.log("未连接")

            await NetWorkManager.Instance.connect()
            return
        }

        const nickname = this.input.string
        if(!nickname){
            console.log("nickname没有")
            return
        }
        //调用callApi方法
        const {success,error,res} = await NetWorkManager.Instance.callApi(ApiMsgEnum.ApiPlayerJoin,{
            nickname,
        });
        if(!success){
            console.error(error)
            return
        }
        DataManager.Instance.myPlayerId = res.player.id
        console.log("res",res)
        director.loadScene(SceneEnum.Hall)
    }
}

